package hu;

import java.util.*;

public class hu45 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        for (int k = 0; k < n; k++) {
            int[] letter = new int[26];
            char[] ch = sc.nextLine().toLowerCase().toCharArray();
            for (int i = 0; i < ch.length; i++) {
                letter[ch[i] - 'a']++;
            }
            int bet = 0;
            Arrays.sort(letter);
            int p = 1;
            for (int i = 0; i < 26; i++) {
                bet += letter[i] * p;
                p++;
            }
            System.out.println(bet);
        }

    }
}
